package com.authorization.project.authorization.service.impl;

import com.authorization.common.utils.DateUtils;
import com.authorization.common.utils.SecurityUtils;
import com.authorization.common.utils.StringUtils;
import com.authorization.project.authorization.domain.Module;
import com.authorization.project.authorization.domain.PackageEntity;
import com.authorization.project.authorization.mapper.ModuleMapper;
import com.authorization.project.authorization.mapper.PackageEntityMapper;
import com.authorization.project.authorization.service.*;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 套餐Service业务层处理
 * 
 * @author 李晓雷
 * @date 2020-04-21
 */
@Service
public class PackageEntityServiceImpl implements IPackageEntityService 
{
    @Autowired
    private PackageEntityMapper packageEntityMapper;

    @Autowired
    private IPackageMoudleMappingService iPackageMoudleMappingService;

    @Autowired
    private IModuleService iModuleService;

    @Autowired
    private IAuthorizationMoudleMappingService iAuthorizationMoudleMappingService;

    @Autowired
    private IAuthorizationMoudleMappingHistoryService iAuthorizationMoudleMappingHistoryService;

    @Autowired
    private ModuleMapper moduleMapper;


    /**
     * 查询套餐
     * 
     * @param id 套餐ID
     * @return 套餐
     */
    @Override
    public PackageEntity selectPackageEntityById(Long id)
    {
        return packageEntityMapper.selectPackageEntityById(id);
    }

    /**
     * 查询套餐列表
     * 
     * @param packageEntity 套餐
     * @return 套餐
     */
    @Override
    public List<PackageEntity> selectPackageEntityList(PackageEntity packageEntity)
    {
        return packageEntityMapper.selectPackageEntityList(packageEntity);
    }

    /**
     * 新增套餐
     * 
     * @param packageEntity 套餐
     * @return 结果
     */
    @Override
    public int insertPackageEntity(PackageEntity packageEntity)
    {
        packageEntity.setCreateBy(SecurityUtils.getUsername());
        packageEntity.setCreateTime(DateUtils.getNowDate());
        return packageEntityMapper.insertPackageEntity(packageEntity);
    }

    /**
     * 修改套餐
     * 
     * @param packageEntity 套餐
     * @return 结果
     */
    @Override
    public int updatePackageEntity(PackageEntity packageEntity)
    {
        packageEntity.setUpdateBy(SecurityUtils.getUsername());
        packageEntity.setUpdateTime(DateUtils.getNowDate());
        return packageEntityMapper.updatePackageEntity(packageEntity);
    }

    /**
     * 批量删除套餐
     * 
     * @param ids 需要删除的套餐ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deletePackageEntityByIds(Long[] ids)
    {
        //删除套餐-模块中表
        for (Long id : ids) {
            iPackageMoudleMappingService.deletePackageMoudleMappingByPackageId(id);
        }

        return packageEntityMapper.deletePackageEntityByIds(ids);
    }

    /**
     * 删除套餐信息
     * 
     * @param id 套餐ID
     * @return 结果
     */
    @Override
    public int deletePackageEntityById(Long id)
    {
        return packageEntityMapper.deletePackageEntityById(id);
    }

    /**
     * 添加套餐-模块信息
     * @param packageEntity
     * @return
     */
    @Override
    @Transactional
    public int insertPackageModule(PackageEntity packageEntity) {
        /**1 添加套餐 **/
        int rest = this.insertPackageEntity(packageEntity);

        /**2 添加套餐-模块中间表 **/
        iPackageMoudleMappingService.bactInsertPackageMoudleMapping(packageEntity.getId(), packageEntity.getModuleIds());
        return rest;
    }

    /**
     * 修改套餐-模块信息
     * @param packageEntity
     * @return
     */
    @Override
    @Transactional
    public int updatePackageModule(PackageEntity packageEntity) {
        /**1 删除套餐-模块中间表 **/
        iPackageMoudleMappingService.deletePackageMoudleMappingByPackageId(packageEntity.getId());

        /**2 添加套餐-模块中间表 **/
        iPackageMoudleMappingService.bactInsertPackageMoudleMapping(packageEntity.getId(), packageEntity.getModuleIds());

        /**3 修改套餐信息 **/
        int rest = this.updatePackageEntity(packageEntity);
        return rest;
    }

    /**
     * 根据产品ID获取套餐、模块集合
     */
    @Override
    public List<PackageEntity> selectProductAnModule(Long productId, String userid) {
        PackageEntity packageEntity = new PackageEntity();
        packageEntity.setProductId(productId);
        List<PackageEntity> packageEntityList = this.selectPackageEntityListByProductId(productId);
        if(packageEntityList!=null && !packageEntityList.isEmpty()){
            packageEntityList.stream().forEach(currentPackageEntity -> {
                currentPackageEntity.setModuleList(iModuleService.selectModule(productId));
            });
        }
        return packageEntityList;
    }

    /**
     * 获取选中的模块集合
     * @param productId
     * @param userid
     * @param histstate =1 历史查询
     * @return
     */
    @Override
    public Map<String, Object> checkedMmoduleList(Long productId, String userid, String histstate, String packageId, String historyId) {
        Map<String, Object> map = new HashMap<>();
        List<Long> checkedModuleIdList = new ArrayList<>();

        if(Objects.equals("1" , histstate)){
            checkedModuleIdList = iAuthorizationMoudleMappingHistoryService.selectMoudleIdsById(historyId);
        }else {
            checkedModuleIdList = iAuthorizationMoudleMappingService.selectMoudleIdsByUserId(userid, productId);

            //所有套餐下配置的选中模块
            List<PackageEntity>  packageList = this.selectCheckedModules(productId);
            map.put("packageList", packageList);

        }
        map.put("checkedModuleIdList", checkedModuleIdList);
        return map;
    }

    /**
     * 根据产品查询所有套餐默认选中的模块
     * @param productId
     * @return
     */
    private List<PackageEntity> selectCheckedModules(Long productId) {
        List<PackageEntity> packageEntityList = new ArrayList<>();
        packageEntityList = this.selectPackageEntityListByProductId(productId);
        packageEntityList.stream().forEach(packageEntity -> {
            List<Long> checkedModuleIds = iPackageMoudleMappingService.selectModuleIdsByPackageId(packageEntity.getId());
            if(checkedModuleIds!=null && checkedModuleIds.size() > 0){
                packageEntity.setCheckedModuleIdList(checkedModuleIds);
            }
        });
        return packageEntityList;
    }

    private List<PackageEntity> selectPackageEntityListByProductId(Long productId) {
        return packageEntityMapper.selectPackageEntityListByProductId(productId);
    }
}
